﻿/*
Name:		art.dialog.js
Version:	1.0.1 Build 2010-01-29
Author: 	tangbin
Email:		admin@planeArt.cn
Website:	www.planeArt.cn
*/

//图片预加载
new Image().src='./dialog.png';//请与CSS图片径保持一致

var art = {};
art.id = function (id){return 'string' == typeof id ? document.getElementById(id) : id;};
art.name = function (name){return document.getElementsByTagName(name);};

//在header添加样式（可把外部CSS整合到javascript中来）
//art.style = {
//	add:function(css,id){
//	var D=document,S=this.style;
//	if(!S){
//		S=this.style=D.createElement('style');
//		S.setAttribute("type","text/css");
//		S.id=id;
//		D.getElementsByTagName('head')[0].appendChild(S);
//	};
//	S.styleSheet&&(S.styleSheet.cssText+=css)||S.appendChild(D.createTextNode(css));
//}};

//样式操作
art.hasClass = function(element,className){
	var reg = new RegExp('(\\s|^)'+className+'(\\s|$)');
	return element.className.match(reg);
};
art.addClass = function(element,className){//添加类
	if(!this.hasClass(element, className)){
		element.className += ' '+className;
	};
};
art.removeClass = function(element,className){//移除类
	if(art.hasClass(element, className)){
		var reg = new RegExp('(\\s|^)'+className+'(\\s|$)');
		element.className = element.className.replace(reg,' ');
	};
};
art.getClass = function(element,attribute){//读取元素CSS属性值
	return element.currentStyle?element.currentStyle[attribute]:document.defaultView.getComputedStyle(element,false)[attribute];   
};

//获取指针坐标
art.mouse = function(event){
	if(event.pageX || event.pageY){
		return {x:event.pageX, y:event.pageY};
	};
	return {
		x:event.clientX + document.body.clientLeft,y:event.clientY + Math.max(document.body.scrollTop,document.documentElement.scrollTop)
	}; 
};

//弹出对话框
art.dialog = function(config){
	var title = config.title;//标题
	var content = config.content;//内容
	var width = config.width;//宽度
	var height = config.height;//高度
	var fn = config.fn;//回调函数
	var closefn = config.closefn; //关闭窗口的回调函数
	var x = config.x;//x轴坐标
	var y = config.y;//y轴坐标
	var style = config.style;//对话框样式
	var fixed = config.fixed;//开启静止定位.默认:true
	var lock = config.lock;//锁屏.默认:false
	var move = config.move;//是否拖动.默认:true
	
	art.close();
	
	//外套
	var mesWrap = document.createElement('div');
	mesWrap.id = mesWrap.className = 'mesWrap';
	if(style != undefined) mesWrap.className = mesWrap.className + ' ' + style;//CSS钩子：添加自定义类
	document.body.appendChild(mesWrap);
	
	//锁屏遮罩
	if(lock){
		fixed = true;
		art.addClass(art.name('html')[0],'mesLock');//CSS钩子：锁屏
		var overlay = document.createElement('div');
		overlay.id = overlay.className = 'mseOverlay';
		overlay.innerHTML = '<div></div>';
		styleStr = (document.all) ? 'filter:alpha(opacity=0);' : 'opacity:0;';
		overlay.style.cssText = styleStr;
		art.id('mesWrap').appendChild(overlay);
		art.showBackground(overlay,100);
	};
	
	//对话框
	var moveFn = (move == false) ? '' : 'unselectable="on" onselectstart="return false" style="-moz-user-select:none;" onmousedown="art.drag(art.id(\'mesWindow\'),event);art.addClass(art.id(\'mesWrap\'),\'mesMove\')" onmouseup="art.removeClass(art.id(\'mesWrap\'),\'mesMove\')"';
	if(parseInt(width) == width) width = width + 'px';//如果不带单位则使用像素为单位
	if(parseInt(height) == height) height = height + 'px';//
	var mesWin=document.createElement('div');
	mesWin.id=mesWin.className='mesWindow';
	mesWin.innerHTML='<table cellspacing="0" cellpadding="0"><tbody><tr><td class="mes-top-left mes-border"> </td><td class="mes-border-top mes-border"> </td><td class="mes-top-right mes-border"> </td></tr><tr><td class="mes-border-left mes-border"> </td><td><table class="mes-body" border="0" cellspacing="0" cellpadding="0"><tr><td class="mes-title-wrap" '+moveFn+'><div id="mes-title" class="mes-title"><div id="mes-title-content" class="mes-title-content">'+title+'</div><a class="mes-title-close" href="javascript:art.close();'+closefn+'">&#20851;&#38381;<span>×</span></a></div></td></tr><tr><td class="mes-content-wrap" style="width:'+width+';height:'+height+';"><div id="mes-conten" class="mes-content">'+content+'</div></td></tr><tr><td class="mes-bottom"><input type="button" value="&#30830;&#23450;" class="mes-button-ok" onclick="'+fn+';" /> <input type="button" value="&#21462;&#28040;" class="mes-button-cancel" onclick="art.close();" /></td></tr></table></td><td class="mes-border-right mes-border"> </td></tr><tr><td class="mes-bottom-left mes-border"> </td><td class="mes-border-bottom mes-border"> </td><td class="mes-bottom-right mes-border"> </td></tr></tbody></table>';//坚固的九宫格布局
	mesWrap.appendChild(mesWin);
	art.range(mesWin,x,y,fixed,false);
	
	if(fixed) art.addClass(art.name('html')[0],'mesFixed');//CSS钩子：告知IE6用CSS处理静止定位BUG
	art.addClass(art.name('html')[0],'mesBegin');//CSS钩子：告知对话框已经弹出，可供CSS处理兼容问题
};

//可视范围修正算法(对象,x轴,y轴,是否静止定位,是否超出可视范围自动重叠在边缘)
art.range = function(obj,x,y,fixed,overlap){
	var left = 0;
	var top = 0; 
	var dd = document.documentElement;
	var db = document.body;
	var ddw = dd.clientWidth;//浏览器内容框宽度
	var ddh = dd.clientHeight;//浏览器内容框高度
	var dbw = db.clientWidth;//页面总宽度
	var dbh = db.clientHeight;//页面总长度
	var st = Math.max(dd.scrollTop, db.scrollTop);//被纵向滚动条卷去的长度
	var sl = Math.max(dd.scrollLeft, db.scrollLeft);//被横向滚动条卷去的长度
	var ow = obj.clientWidth;//对话框宽度
	var oh = obj.clientHeight;//对话框高度

	if(fixed){
		var minX = 0;
		var maxX = ddw - ow;
		var centerX = maxX / 2;
		var minY = 0;
		var maxY = ddh - oh;
		var centerY = (ddh * 0.382 - oh / 2 > 0) ?  ddh * 0.382 - oh / 2 : maxY / 2;//黄金比例算法
	}else{
		var minX = sl;
		var maxX = ddw + minX - ow;
		var centerX = maxX / 2;
		var minY = st;
		var maxY = ddh + minY - oh;
		var centerY =  (ddh * 0.382 - oh / 2 + minY > minY) ? ddh * 0.382 - oh / 2 + minY : (maxY + minY) / 2;//黄金比例算法
	};
	if(overlap){
		if(y > maxY) y = maxY;
		if(x > maxX) x = maxX;
		//优先满足最小坐标限制，防止对象超过可是面积而被截取
		if(x < minX) x = minX;
		if(y < minY) y = minY;
		left = x;
		top = y;
	}else{
		if(x == undefined || x == 'center'){		
			left = centerX;
		}else
		if(x == 'left'){
			left = minX;
		}else
		if(x == 'right'){
			left = maxX;
		}else{
			if(fixed)x = x - sl;//把原点移到浏览器视口
			if(x < minX) x = x + ow;
			if(x > maxX) x = x - ow;
			left = x;
		};
		if(y == undefined || y == 'center'){
			top = centerY;
		}else
		if(y == 'top'){
			top = minY;
		}else
		if(y == 'bottom'){
			top = maxY;
		}else{
			if(fixed)y = y - st;//把原点移到浏览器视口
			if(y < minY) y = y + oh;
			if(y > maxY) y = y - oh;
			top = y;
		};
	};
	//obj.style.cssText = 'left:' + left + 'px;top:' + top + 'px;';//此写法 IE6 会抖动.性能问题？
	obj.style.left = left + 'px';
	obj.style.top = top + 'px';
};

//让遮罩渐渐变暗 
art.showBackground = function(obj,endInt){
	if(document.all){
		obj.filters.alpha.opacity += 20;
		if(obj.filters.alpha.opacity<endInt) setTimeout(function(){art.showBackground(obj,endInt)},5);
	}else{
		var al=parseFloat(obj.style.opacity);al += 0.20;
		obj.style.opacity=al;
		if(al < (endInt/100)) setTimeout(function(){art.showBackground(obj,endInt)},5);
	};
};

//关闭弹出层
art.close = function(){
	var html = art.name('html')[0];
	var mw = art.id('mesWrap');
	if(mw!=null) mw.parentNode.removeChild(mw);
	//移除CSS钩子
	art.removeClass(html,'mesLock');
	art.removeClass(html,'mesFixed');
	art.removeClass(html,'mesBegin');
};

//ESC键关闭弹出层
document.onkeyup = function(evt){
	var evt = evt || window.event;
	if(evt.keyCode == 27) art.close();
};

//拖动元素
art.drag = function(o,e){
	if((o.style.top && o.style.left) == false) return false;
	var event = window.event || e;
	var _sy = event.clientY;
	var _sx = event.clientX;
	var _xy = parseInt(o.style.top);
	var _xx = parseInt(o.style.left);
	document.onmouseup = function(){
		this.onmousemove = null;
	};
	if(event.preventDefault) event.preventDefault();
	document.onmousemove = function(e){
		var event = window.event || e;
		if(document.all && event.button == 0){
			this.onmousemove = null;
			return false;
		};
		var _top = _xy + event.clientY - _sy;
		var _left = _xx + event.clientX - _sx;

		var fixed = ((art.name('html')[0].className.indexOf('mesFixed') > -1) || (art.getClass(o,'position')=='fixed')) ? true : false;
		art.range(o,_left,_top,fixed,true);
	};
};
